//
//  LeftViewController.m
//  Buiom_7.0
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 triffe. All rights reserved.
//

#import "LeftViewController.h"
#import "AvoidResponseVC.h"

@interface LeftViewController ()<UIAlertViewDelegate>

@property (nonatomic, strong) UILabel *lable;

@property (nonatomic, strong) UIButton *cachesButton;

@property (nonatomic, strong) UIImageView *imageView;


@property (nonatomic, strong) UIButton *avoidResponseButton;

@property (nonatomic, assign) CGPoint startPoint;

@property (nonatomic, strong) UIButton *collectButton;

// 夜间模式的控件,
@property (nonatomic, strong)UIButton  *nightButton;
@property (nonatomic, assign) BOOL isClick;


@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 100, 40)];
//    self.lable.text = @"左边菜单栏";
//    self.lable.textAlignment = NSTextAlignmentCenter;
//    self.lable.textColor = [UIColor whiteColor];
    self.isClick = YES;
    
    self.imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.imageView.image = [UIImage imageNamed:@"smallleft.jpg"];
    self.imageView.userInteractionEnabled = YES;
    [self.view addSubview:self.imageView];
    
    self.cachesButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.cachesButton.frame = CGRectMake((kScreenWidth - 100) / 2, kScreenHeight / 3 - 100, 100, 100);
    [self.cachesButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:(UIControlStateNormal)];
    [self.cachesButton setTitle:@"清除缓存" forState:(UIControlStateNormal)];
    [self.cachesButton addTarget:self action:@selector(actionCachesButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.imageView addSubview:self.cachesButton];
    
    
    self.avoidResponseButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.avoidResponseButton.frame = CGRectMake((kScreenWidth - 100) / 2 - 100, kScreenHeight / 3 , 100, 100);
    [self.avoidResponseButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:(UIControlStateNormal)];
    [self.avoidResponseButton setTitle:@"免责声明" forState:(UIControlStateNormal)];
    [self.avoidResponseButton addTarget:self action:@selector(actionAvoidButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.imageView addSubview:self.avoidResponseButton];
    
    
    self.collectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.collectButton.frame = CGRectMake((kScreenWidth - 100) / 2, kScreenHeight / 3 + 100, 100, 100);
    [self.collectButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:(UIControlStateNormal)];
    [self.collectButton setTitle:@"我的收藏" forState:(UIControlStateNormal)];
    [self.collectButton addTarget:self action:@selector(actionCollectButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.imageView addSubview:self.collectButton];
    
    self.nightButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.nightButton.frame = CGRectMake((kScreenWidth - 100) / 2 - 100, kScreenHeight / 3 + 200, 100, 100);
    [self.nightButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:(UIControlStateNormal)];
    [self.nightButton setTitle:@"开:夜间模式" forState:(UIControlStateNormal)];
    [self.nightButton addTarget:self action:@selector(nightButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.imageView addSubview:self.nightButton];
   // [self.view addSubview:self.lable];
    
    
    
    
    // Do any additional setup after loading the view.i
}
// 夜间模式的开关
- (void)nightButtonAction:(UIButton *)button
{
    if (_isClick == YES) {
        [self.nightButton setTitle:@"关:夜间模式" forState:(UIControlStateNormal)];
        [[UIApplication sharedApplication].delegate window].alpha = 0.3;
        _isClick = NO;
    }  else {
        [self.nightButton setTitle:@"开:夜间模式" forState:(UIControlStateNormal)];
        [[UIApplication sharedApplication].delegate window].alpha = 1;
        _isClick = YES;
    }
 
}


- (void)actionCachesButton:(UIButton *)button
{
    [[SDImageCache sharedImageCache] clearDisk];
    float tmpSize = [[SDImageCache sharedImageCache] checkTmpSize];
    NSString *clearCachName = tmpSize >= 1 ?[NSString stringWithFormat:@"清理缓存(%.2fM)",tmpSize] : [NSString stringWithFormat:@"清理缓存(%.2fK)",tmpSize * 1024];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:clearCachName delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
}


- (void)actionAvoidButton:(UIButton *)button
{
    AvoidResponseVC *avoidVC = [[AvoidResponseVC alloc] init];
    UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:avoidVC];
    // 模态动画
    navC.modalPresentationStyle = UIModalPresentationFullScreen;
    navC.modalTransitionStyle = UIModalTransitionStylePartialCurl;

    [self presentViewController:navC animated:YES completion:nil];
}


- (void)actionCollectButton:(UIButton *)button
{
    
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self.cachesButton];
    self.startPoint = point;
}

//- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    CGPoint point = [[touches anyObject] locationInView:self.cachesButton];
//    float dx = point.x - _startPoint.x;
//    float dy = point.y - _startPoint.y;
//    //计算移动后的view中心点
//    CGPoint newcenter = CGPointMake(self.cachesButton.center.x + dx, self.cachesButton.center.y + dy);
//    
//    
//    /* 限制用户不可将视图托出屏幕 */
//    float halfx = CGRectGetMidX(self.cachesButton.bounds);
//    //x坐标左边界
//    newcenter.x = MAX(halfx, newcenter.x);
//    //x坐标右边界
//    newcenter.x = MIN(self.view.bounds.size.width - halfx, newcenter.x);
//    
//    //y坐标同理
//    float halfy = CGRectGetMidY(self.cachesButton.bounds);
//    newcenter.y = MAX(halfy, newcenter.y);
//    newcenter.y = MIN(self.view.bounds.size.height - halfy, newcenter.y);
//    
//    //移动view
//    self.cachesButton.center = newcenter;
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
